﻿using Handy_DNS.View.Pages;
using HandyControl.Controls;
using HandyControl.Data;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using Handy_Domain.DNS.Model;
using Handy_WPF.MVVM;
using Kxx_Domain.DNS.Model;
using Kxx_Services.DNS;
using Kxx_Save.DNS;

namespace Handy_DNS.ViewModel
{
    /// <summary>
    /// 域名ViewModel
    /// </summary>
    public class DomainViewModel : PageBaseViewModel<DomainModel>
    {
        DNSService dnsService;
        Action actionDataRefresh;

        public DomainViewModel()
        {
            dnsService = new DNSService();

            actionDataRefresh = new Action(() => {
                this.IsRunning = true;
                DataBind();
            });
            new Task(actionDataRefresh).Start();
        }

        #region 数据绑定
        /// <summary>
        /// 所有数据
        /// </summary>
        private ObservableCollection<DomainModel> _totalDataList = new ObservableCollection<DomainModel>();

        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="isNew">是否获取最新数据</param>
        private void DataBind(bool isNew = false)
        {
            PageBaseModel<DomainModel> pageModel = dnsService.GetDNSList(PageIndex, PageSize, isNew);
            base.PageIndex = pageModel.PageNumber;
            base.PageSize = pageModel.PageSize;
            base.TotalCount = pageModel.TotalCount;
            this._totalDataList = pageModel.list;

            base.DataList = _totalDataList.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
        }
        #endregion

        #region 命令
        private BaseCommand pageUpdatedCommand;
        /// <summary>
        /// 页码改变命令
        /// </summary>
        public BaseCommand PageUpdatedCommand
        {
            get
            {
                if (pageUpdatedCommand == null)
                {
                    pageUpdatedCommand = new BaseCommand(new Action<object>(o =>
                    {
                        int index = ((FunctionEventArgs<int>)o).Info;
                        base.PageIndex = index;
                        DataList = _totalDataList.Skip((index - 1) * PageSize).Take(PageSize).ToList();
                    }));
                }
                return pageUpdatedCommand;
            }
        }

        private BaseCommand refreshCommand;
        /// <summary>
        /// 刷新
        /// </summary>
        public BaseCommand RefreshCommand
        {
            get
            {
                if (refreshCommand == null)
                {
                    refreshCommand = new BaseCommand(new Action<object>(o =>
                    {
                        new Task(actionDataRefresh).Start();
                    }));
                }
                return refreshCommand;
            }
        }
        #endregion
    }
}
